package test.com.jse;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;


import com.jse.Js;
import com.jse.Jse;
import com.jse.Lang;
import com.jse.Log;
import com.jse.js.JavaScriptObject;
import com.jse.js.QuackContext;
import com.jse.js.QuackPromise;
import com.jse.js.QuackPromiseReceiver;
import com.jse.json.Json;

public class QuackJs {

	public static void main(String[] args) throws Exception {
//		System.out.println(System.getProperty("user.dir"));
//		QuackContext quack = QuackContext.create(true);
//		 quack.getGlobalObject().set("dzh", "你好好");//公共
//        var a=quack.evaluate("function tcp(str) {return `_${str}._tcp`;}tcp", "script.js");
//        System.out.println(quack.evaluateForJavaScriptObject("tcp").call(22));
//        var b=quack.evaluate("function tcp(str) {return `_${str}._tcp`;}tcp(33)", "script.js");
//        System.out.println(b);
//        quack.close();
		Log log=Log.get("JS");
		QuackContext quack=QuackContext.create(true);
		var global=quack.getGlobalObject();
		global.set("console", log);
		global.set("System",System.class);
		global.set("Lang",Lang.class);
		quack.evaluate("var print=(x)=>{System.out.println(x)}");
		var main=quack.compileFunction("function main(){ return 1+1;}","/a/b/1");
		var main1=quack.compileFunction("function main(){ return 2+1;}","/a/b/2");
		
		
		System.out.println("main="+quack.evaluateForJavaScriptObject("main()"));
//		System.out.println("main1="+quack.evaluate("main()","/a/b/2"));
//		quack.evaluate("function main(){ 1+1;}");
		Js.eval("function main(){ return 1+1;}");//1939
		
//		var main=quack.evaluateForJavaScriptObject("main");
		
//		Object rt=main.call(quack.coerceJavaToJavaScript(Supplier.class,new Supplier<Object>() {
//			@Override
//			public Object get() {
//				System.out.println("get java method");
//				return null;
//			}
//		}));
//		System.out.println(quack.coerceJavaScriptToJava(int.class,rt));
//		var v8Runtime = com.caoccao.javet.interop.V8Host.getV8Instance().createV8Runtime();
//		v8Runtime.getExecutor("function main(){ return 1+1;}").executeVoid();
		long s1=System.currentTimeMillis();
		for (int i = 0; i < 1; i++) {
//			quack.evaluate("function main(){ return 1+1;}main()");
//			main.call();
//			java.util.function.Supplier<Integer> a=()->{return 1+1;};
//			a.get();
//			Js.eval("function main(){return 1+1;}main()");
//			Js.invokeFunction("main");
//			v8Runtime.getGlobalObject().invokeVoid("main");
			
//			quack.putJavaToJavaScriptCoercion(Function.class, (clazz, o) -> "hello world");//不知道
			
			
//			String script = "new Promise((resolve, reject) => { resolve('hello'); });";
//	        JavaScriptObject jo = quack.evaluateForJavaScriptObject(script);
//	        QuackPromise promise = jo.proxyInterface(QuackPromise.class);
//
//	        Semaphore semaphore = new Semaphore(0);
//	        promise.then(new QuackPromiseReceiver(){
//	            @Override
//	            public void receive(Object o) {
//	            	System.out.println("r="+o);
//	                semaphore.release();
//	            }
//	        });
//	        semaphore.tryAcquire(500, TimeUnit.MILLISECONDS);
	        /** JavaClass=Class.class
	         * var Foo2 = JavaClass.forName('com.jse.js.TestQuack$Foo2');\n" +
                        "var foo = new Foo2();
	         */
		}
		long s2=System.currentTimeMillis();//print(Lang.uuid());
		
		System.out.println(Jse.pid+"=="+(s2-s1)+"=="+Runtime.getRuntime().freeMemory());
//		JavaScriptObject func = quack.compileFunction("function(ret) { return ret; }", "?");
//		quack.close();
		while (true) {
			
		}
//		var x=func.call("a");
//		System.out.println(func);
	}
}
